﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataTransferObject;
using Model;
using System.Data;

namespace DataService
{
    public class StudentService
    {        
        private DataAccess dataAccess;
        public delegate void NotifyAddNewStudentHandler(Student student);
        public NotifyAddNewStudentHandler NotifyAddNewStudent;

        public StudentService()
        {
            dataAccess = new DataAccess();
        }

        public IList<Student> GetAllStudents()
        {
            DataView view = dataAccess.StudentsDataView;
            IList<Student> studentList = new List<Student>();
            foreach (DataRowView row in view)
            {
                StudentDataSet.StudentRow studentRow = row.Row as StudentDataSet.StudentRow;
                studentList.Add(new Student(studentRow.Id, studentRow.Name, studentRow.StudentCode, studentRow.ClassName, studentRow.Sex));
            }
            
            return studentList;
        }

        public void AddNewStudent(Student student)
        {
            if (student == null)
            {
                throw new ArgumentNullException("student");
            }

            StudentDataSet.StudentRow row = dataAccess.CreateNewRow();
            row.Id = dataAccess.getNextStudentId();            
            row.Name = student.Name;
            row.StudentCode = student.StudentCode;
            row.ClassName = student.ClassName;

            dataAccess.AddNewRow(row);

            Student newStudent = new Student(row.Id, student.Name, student.StudentCode, student.ClassName, student.Sex);
            if (NotifyAddNewStudent != null)
            {
                NotifyAddNewStudent(newStudent);
            }
        }
   
   
    }
}
